package com.ald.crossex.action;

import com.ald.crossex.dto.LoginRequest;
import com.ald.crossex.dto.LoginResponse;
import com.ald.crossex.dto.Principle;
import com.ald.crossex.entity.TCeCoreUser;
import com.ald.crossex.service.UserService;
import com.ald.crossex.util.ErrorCode;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;

/**
 * Created by lianghaijian on 2014-03-31.
 */
@Controller
@RequestMapping(value = "/")
public class PublicAction extends BaseAction {

    @Autowired
    private UserService userService;

    private static Logger log = Logger.getLogger(PublicAction.class);

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String loginPage(HttpServletRequest request, HttpServletResponse response) {
//        if("le".equals(request.getParameter("locale"))){
//            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.US);
//        }else{
//            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.CHINA);
//        }
        return "/public/login";
    }

    /**
     * for test
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "admin/main", method = RequestMethod.GET)
    public String homePage(HttpServletRequest request, HttpServletResponse response) {
//        if("le".equals(request.getParameter("locale"))){
//            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.US);
//        }else{
//            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,Locale.CHINA);
//        }
//        TCeCoreUser user = new TCeCoreUser();
//        user.setUid(1);
//        request.getSession().setAttribute(Principle.PRINCIPLE_SESSION_ATTRIBUTE_USER, new Principle(user));
        return "/admin/index";
    }

    /**
     * login method
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public LoginResponse login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest, Locale locale) {
        LoginResponse response = new LoginResponse();
        try {
            TCeCoreUser user = userService.getTCeCoreUserByUsernameAndPassword(request.getUsername(), request.getPassword());
            if (user == null) {
                response.setResultCode(ErrorCode.OPTION_ERROR.getCode());
                response.setResultMsg("用户不存在或账号密码错误！");
            } else {
                httpServletRequest.getSession().setAttribute(Principle.PRINCIPLE_SESSION_ATTRIBUTE_USER, new Principle(user));
                response.setResultCode(ErrorCode.OPTION_SUCCESS.getCode());
            }
        } catch (Exception e) {
            log.error("login error.username:" + request.getUsername(), e);
            response.setResultCode(ErrorCode.INTERNAL_EXCEPTION.getCode());
            response.setResultMsg(ErrorCode.INTERNAL_EXCEPTION.getMessage());

        }
        return response;
    }

    @RequestMapping(value = "logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().removeAttribute(Principle.PRINCIPLE_SESSION_ATTRIBUTE_USER);
        response.sendRedirect(request.getContextPath());
    }
}
